package com.uwin.findTomcat;

import java.io.Serializable;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

@SuppressWarnings("serial")
public class Servicer implements Serializable {

	private RemoteExecuteCommand rec;
	private Free free;
	private List<PsCmd> psCmds;
	private String remark;
	
	public Servicer(String ip,String user,String passwd,Integer port,String remark){
		this.rec = new RemoteExecuteCommand(ip,user,passwd,port);
		free = Free.reslvos(rec);
		psCmds = PsCmd.resolves(rec);
		this.remark = remark;
	}
	
	public Servicer(RemoteExecuteCommand rec){
		this.rec = rec;
		free = Free.reslvos(rec);
		psCmds = PsCmd.resolves(rec);
	}
	
	/**
	 * IP
	 * Free
	 * total       used       free     shared    buffers     cached
	 * 数据
	 * 
	 * tomcat
	 * tomcatname path mem%
	 */
	public void writeSheet(HSSFSheet sheet){
		HSSFRow row1 = null;
		//第一行写IP
		row1 = sheet.createRow(0);
		row1.createCell(0).setCellValue(rec.getIp());
		row1.createCell(1).setCellValue(remark);
		//第二行 FREE 内存信息 标题
		row1 = sheet.createRow(1);
		row1.createCell(0).setCellValue("内存使用情况");
		//第三行 内存信息 th
		row1 = sheet.createRow(2);
		row1.createCell(0).setCellValue("total");
		row1.createCell(1).setCellValue("used");
		row1.createCell(2).setCellValue("free");
		row1.createCell(3).setCellValue("cached");
		//第四行 内存信息 数据
		row1 = sheet.createRow(3);
		row1.createCell(0).setCellValue(free.getTotal());
		row1.createCell(1).setCellValue(free.getUsed());
		row1.createCell(2).setCellValue(free.getFree());
		row1.createCell(3).setCellValue(free.getCached());
		//空2行
		row1 = sheet.createRow(5);
		row1.createCell(0).setCellValue("tomcat");
		row1 = sheet.createRow(6);
		row1.createCell(0).setCellValue("tomcat PID");
		row1.createCell(1).setCellValue("tomcatName");
		row1.createCell(2).setCellValue("tomcat占用内存");
		row1.createCell(3).setCellValue("tomcat路径");
		row1.createCell(4).setCellValue("占用端口信息");
		
		int i=7;
		for(PsCmd cmd : psCmds){
			row1 = sheet.createRow(i++);
			row1.createCell(0).setCellValue(cmd.getPid());
			row1.createCell(1).setCellValue(cmd.getTomcatName());
			row1.createCell(2).setCellValue(cmd.getMem());
			row1.createCell(3).setCellValue(cmd.getTomcatPath());
			row1.createCell(4).setCellValue(cmd.getAddresses());
		}
	}
	
	public Free getFree() {
		return free;
	}
	public List<PsCmd> getPsCmds() {
		return psCmds;
	}

	public RemoteExecuteCommand getRec() {
		return rec;
	}
	
}
